package com.wantong.jianpu.viewmodels;

import android.app.Application;

import androidx.annotation.NonNull;

import com.wantong.jianpu.base.BaseResultBean;
import com.wantong.jianpu.beans.FaultCommitBean;
import com.wantong.jianpu.beans.MeasuresSelectBean;
import com.wantong.jianpu.beans.PersonBean;
import com.wantong.jianpu.beans.UpLoadBean;
import com.wantong.jianpu.beans.UploadFileBean;
import com.wantong.jianpu.event.FaultDetailEvent;
import com.wantong.jianpu.models.FaultModel;
import com.wantong.jianpu.models.UpLoadModel;
import com.wantong.jianpu.net.BaseObserver;

import org.greenrobot.eventbus.EventBus;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import me.goldze.mvvmhabit.base.BaseViewModel;
import me.goldze.mvvmhabit.binding.command.BindingCommand;
import me.goldze.mvvmhabit.bus.event.SingleLiveEvent;
import me.goldze.mvvmhabit.utils.JsonUtil;
import me.goldze.mvvmhabit.utils.KLog;
import me.goldze.mvvmhabit.utils.StringUtils;
import me.goldze.mvvmhabit.utils.ToastUtils;

/**
 * @author PACY
 * @date 2022/5/6
 * description:
 */
public class FaultProViewModel extends BaseViewModel {
    public SingleLiveEvent<Integer> type=new SingleLiveEvent<>();
    public SingleLiveEvent<String> state=new SingleLiveEvent<>();//维修状态
    public SingleLiveEvent<String[]> stateDialogEvent=new SingleLiveEvent<>();
    public SingleLiveEvent<String> repairTypeText=new SingleLiveEvent<>();//维修方式
    public SingleLiveEvent<String[]> repairDialogEvent=new SingleLiveEvent<>();
    public SingleLiveEvent<String[]> natureDialogEvent=new SingleLiveEvent<>();//维修性质
    public SingleLiveEvent<String> natureText=new SingleLiveEvent<>();//维修性质

    public SingleLiveEvent<String> measuresText=new SingleLiveEvent<>();//处理措施
    public SingleLiveEvent<String> price=new SingleLiveEvent<>();//维修金额


    public FaultModel model;
    public UpLoadModel upLoadModel;

    //维修人员
    public List<PersonBean> userBeans;
    public SingleLiveEvent<String> user=new SingleLiveEvent<>();
    public SingleLiveEvent<String[]> userDialogEvent=new SingleLiveEvent<>();
    //故障分析下拉
    public List<MeasuresSelectBean> measuresBeans;
    public SingleLiveEvent<String> measures=new SingleLiveEvent<>();
    public SingleLiveEvent<String[]> measuresDialogEvent=new SingleLiveEvent<>();

    //需要提交字段
    public String repairOrderId;//报修id
    private int status;//维修状态  4维修中  5暂不维修  7已修复
    public SingleLiveEvent<String> remark=new SingleLiveEvent<>();//备注信息（暂不维修时需要填写）
    public int repairUser;//维修人员id
    public int repairType;//维修方式 1现场维修 2电话指导
    public String confMeasuresId;//故障分析id
    public int nature;//维修性质 1一般 2抢险
    public List<UploadFileBean> files=new ArrayList<>();//图片集合

    public FaultProViewModel(@NonNull Application application) {
        super(application);
        model=new FaultModel();
        upLoadModel=new UpLoadModel();
        type.setValue(0);
    }

    @Override
    protected void init() {
        super.init();
        getUserRequest();
        getMeasuresRequest();
    }


    /**
     * 提交点击事件
     */
    public BindingCommand commit=new BindingCommand(() -> {
        if(status==0){
            ToastUtils.show("请选择维修状态");
            return;
        }

        if(status==4){
           if(repairType==0){
               ToastUtils.show("请选择维修方式");
               return;
           }
        }

        if(status==7){
            if(repairType==0){
                ToastUtils.show("请选择维修方式");
                return;
            }

            if(StringUtils.isEmpty(confMeasuresId)){
                ToastUtils.show("请选择故障分析");
                return;
            }

            if(nature==0){
                ToastUtils.show("请选择维修性质");
                return;
            }

            if(StringUtils.isEmpty(measuresText.getValue())){
                ToastUtils.show("请输入处理措施");
                return;
            }
            if(StringUtils.isEmpty(price.getValue())){
                ToastUtils.show("请输入维修金额");
                return;
            }
        }

        if(status==5){
            if(StringUtils.isEmpty(remark.getValue())){
                ToastUtils.show("请输入原因说明");
                return;
            }
        }

       commitRequest();


    });


    /**
     * 维修状态
     */
    public BindingCommand selectState=new BindingCommand(() -> {
        stateDialogEvent.setValue(new String[]{"维修中", "已修复","暂不修复"});
    });

    /**
     * 设置维修状态
     * @param str
     */
    public void setState(String str){
        state.setValue(str);
        if(str.equals("维修中")){
            status=4;
        }else if(str.equals("已修复")){
            status=7;
        }else if(str.equals("暂不修复")){
            status=5;
        }
    }


    /**
     * 维修方式
     */
    public BindingCommand selectRepairType=new BindingCommand(() -> {
        repairDialogEvent.setValue(new String[]{"现场维修", "电话指导"});
    });

    /**
     * 设置维修方式
     * @param str
     */
    public void setRepairType(String str){
        repairTypeText.setValue(str);
        if(str.equals("现场维修")){
            repairType=1;
        }else if(str.equals("电话指导")){
            repairType=2;
        }
    }


    /**
     * 筛选协助人员
     */
    public BindingCommand selectUser=new BindingCommand(() -> {
        if (userBeans!= null) {
            String[] brand = new String[userBeans.size()];
            for (int i = 0; i < userBeans.size(); i++) {
                brand[i] = userBeans.get(i).getName();
            }
            userDialogEvent.setValue(brand);
        } else {
            getUserRequest();
        }
    });
    /**
     * 协助人员选择
     */
    public void setUserPosition(int position) {
        if (userBeans != null) {
            user.setValue(userBeans.get(position).getName());
            repairUser=userBeans.get(position).getId();
        }
    }


    /**
     * 故障分析选择
     */
    public BindingCommand selectMeasures=new BindingCommand(() -> {
        if (measuresBeans!= null) {
            String[] brand = new String[measuresBeans.size()];
            for (int i = 0; i < measuresBeans.size(); i++) {
                brand[i] = measuresBeans.get(i).getName();
            }
            measuresDialogEvent.setValue(brand);
        } else {
            getMeasuresRequest();
        }
    });
    /**
     * 筛选故障分析
     */
    public void setMeasuresPosition(int position) {
        if (measuresBeans != null) {
            measures.setValue(measuresBeans.get(position).getName());
            confMeasuresId=measuresBeans.get(position).getId();
        }
    }



    /**
     * 维修性质
     */
    public BindingCommand selectNature=new BindingCommand(() -> {
        natureDialogEvent.setValue(new String[]{"一般", "抢险"});
    });

    /**
     * 设置维修性质
     * @param str
     */
    public void setNatureType(String str){
        natureText.setValue(str);
        if(str.equals("一般")){
            nature=1;
        }else if(str.equals("抢险")){
            nature=2;
        }
    }


    /**
     * 获取维修人员
     */
    public void getUserRequest(){
        showDialog();
        model.userList(new BaseObserver<List<PersonBean>>() {
            @Override
            public void onSuccess(BaseResultBean<List<PersonBean>> t) {
                userBeans=t.getData();
            }
            @Override
            public void onComplete() {
                super.onComplete();
                dismissDialog();
            }
        },getLifecycleProvider());
    }

    /**
     * 获取故障分析
     */
    public void getMeasuresRequest(){
        showDialog();
        model.listAll(new BaseObserver<List<MeasuresSelectBean>>() {
            @Override
            public void onSuccess(BaseResultBean<List<MeasuresSelectBean>> t) {
                measuresBeans=t.getData();
            }
            @Override
            public void onComplete() {
                super.onComplete();
                dismissDialog();
            }
        },getLifecycleProvider());
    }


    /**
     * 提交
     */
    public void commitRequest(){
        Map<String,Object> map=new HashMap<>();
        map.put("repairOrderId",repairOrderId);
        map.put("status",status);
        if(status==4){//维修中
            map.put("repairType",repairType);
            if(repairUser!=0){
                map.put("helpUsers",repairUser);
            }
            map.put("repairFiles", files);
        }

        if(status==7){//已修复
            map.put("repairType",repairType);
            map.put("confMeasuresId",confMeasuresId);
            map.put("nature",nature);
            map.put("measures",measuresText.getValue());
            map.put("repairPrice",price.getValue());
            map.put("repairFiles", files);
        }

        if(status==5){//暂不修复
            map.put("remark",remark.getValue());
        }

        showDialog();
        model.commitRepairProgress(map, new BaseObserver() {
            @Override
            public void onSuccess(BaseResultBean t) {
                ToastUtils.show("提交成功");
                EventBus.getDefault().post(new FaultDetailEvent());
                finish();
            }

            @Override
            public void onComplete() {
                super.onComplete();
                dismissDialog();
            }
        },getLifecycleProvider());
    }



    /**
     * 上传文件
     * @param filePath
     */
    public void upLoadFile(List<String> filePath){
        showDialog();
        upLoadModel.upLoadImages(filePath, new BaseObserver<List<UpLoadBean>>() {
            @Override
            public void onSuccess(BaseResultBean<List<UpLoadBean>> t) {
                for (UpLoadBean datum : t.getData()) {
                    UploadFileBean fileBean=new UploadFileBean();
                    fileBean.setUrl(datum.getUrl());
                    fileBean.setFileName(datum.getFileName());
                    files.add(fileBean);
                }
            }

            @Override
            public void onComplete() {
                super.onComplete();
                dismissDialog();
            }
        },getLifecycleProvider());
    }
}
